package com.wzg.appframe.action;

import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.wzg.appframe.net.VolleyRequest;
import com.wzg.appframe.utils.AesUtil;
import com.wzg.appframe.utils.GsonUtil;
import com.wzg.appframe.utils.SharedPreferencesUtils;
import com.wzg.appframe.utils.TLog;
import com.wzg.appframe.utils.ToastUtil;
import com.wzg.appframe.view.BaseActivity;

import java.util.Map;

public abstract class BaseAction {
    public static final String API_URL_KEY = "API_URL_KEY";
    public static final String API_URL = "http://117.80.217.22:19000/sc-api-gateway/frame/sc-frame/";//"218.93.242.54:8888";
    public static final String APP_H5_URL = "APP_H5_URL";
    public static final String APP_UPDATE_URL = "APP_UPDATE_URL";
        public static String H5_URL = "http://192.168.1.5:8080/dist/index.html";
//        public static String H5_URL = "https://www.baidu.com";
//    public String UPDATE_URL = IP + "/WebService/app/update.json";
    public BaseActivity activity;

    public BaseAction(Context context) {
        this.activity = (BaseActivity) context;
        updateIp();
    }

    /**
     * 初始化接口地址
     */
    public void updateIp() {
        String apiUrl = getSharedPreferenData(API_URL_KEY);
        if (null == apiUrl || "".equals(apiUrl.trim())) {
            apiUrl = API_URL;
            setSharedPreferenData(API_URL_KEY, apiUrl);
        }
    }

    /**
     * 获取当前本地apk的版本
     *
     * @return
     */
    public int getVersionCode() {
        int versionCode = 0;
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCode = activity.getPackageManager().
                    getPackageInfo(activity.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取版本号名称
     *
     * @return
     */
    public String getVerName() {
        String verName = "";
        try {
            verName = activity.getPackageManager().
                    getPackageInfo(activity.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

    public String getSharedPreferenData(String key) {
        SharedPreferencesUtils helper = new SharedPreferencesUtils(this.activity, "setting");
        String value = helper.getString(key);
        return value;
    }

    public Map<String, ?> getAll() {
        SharedPreferencesUtils helper = new SharedPreferencesUtils(this.activity, "setting");
        return helper.getAll();
    }

    public void setSharedPreferenData(String key, String value) {
        SharedPreferencesUtils helper = new SharedPreferencesUtils(this.activity, "setting");
        helper.putValues(new SharedPreferencesUtils.ContentValue(key, value));
    }

    public void post(final String method, JsonObject parmaObject) {
        activity.showDialog("请求数据...");
        String apiUrl = getSharedPreferenData(API_URL_KEY);
        VolleyRequest request = new VolleyRequest(Request.Method.POST, apiUrl + method, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                TLog.d("response=" + response);
                JsonObject jsonObject = GsonUtil.getObject(response);
                String code = jsonObject.get("code").getAsString();

                if ("1".equals(code)) {
                    JsonElement data = jsonObject.get("data");
                    postCallBack(method, data);
                } else {
                    String des = jsonObject.get("des").getAsString();
                    ToastUtil.show(des);
                }
                activity.hideDialog();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("tracy", "onErrorResponse=" + error.getMessage());
                activity.hideDialog();
                ToastUtil.show("数据获取失败，原因：" + error.getMessage());
            }
        }, this.activity);

        try {
            request.reqBody = AesUtil.encrypt(GsonUtil.jsonObjectToJsonStr(parmaObject));
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.start();
    }

    public abstract void postCallBack(String method, Object data);

}
